import {App} from "@/model/App.ts";

export class CacheApp {
    private _id: string;
    private _name: string;
    private _url: string;
    private _selected: boolean;

    constructor(id: string, name: string, url: string, selected: boolean) {
        this._id = id;
        this._name = name;
        this._url = url;
        this._selected = selected;
    }
    set id(value: string){
       this._id = value;
   }
   set name(value: string){
        this._name = value;
   }
   set url(value: string){
       this._url = value;
   }
   set selected(value: boolean){
        this._selected = value;
   }
    get id(): string {
        return <string>this._id;
    }
    get name(): string {
        return <string>this._name;
    }
    get url(): string {
        return <string>this._url;
    }
    get selected(): boolean {
        return <boolean>this._selected;
    }
    toJSON() {
        return {
            id: this._id,
            name: this._name,
            url: this._url,
            selected: this._selected
        };
    }

}

export class LocalStorage {

    private _option: string;

    private _app: CacheApp;

    constructor(option: string, app?: App) {
        this._option = option;
        if (app) {
            this._app = new CacheApp(app.id, app.name, app.url, app.selected);
        }else{
            this._app = new CacheApp('', '', '', false);
        }
    }

    set option(value: string) {
        this._option = value;
    }

    set app(value: CacheApp) {
        this._app = value;
    }

    get option(): string {
        return this._option;
    }

     get app(): CacheApp {
        return this._app as CacheApp;
    }

    get store(): Array<CacheApp> {
        // 获取缓存数据, 判断是否存在, 存在则删除, 然后更新缓存
        return JSON.parse(localStorage.getItem('selectedApps') || '[]');
    }

     updateCache(): Array<CacheApp> {
        // 获取缓存数据, 判断是否存在, 存在则删除, 然后更新缓存
        const storedApps: Array<CacheApp> = this.store;
        const exists = storedApps.some((store: CacheApp) => store.id === this.app.id);
        if (this.option === 'select') {
            // 将所有storedApps中对象的selected设置为false, 然后将当前app设置为true
            storedApps.forEach((store: CacheApp) => {
                store.selected = false;
            })
            if (!exists) {
                this.app.selected = true;
                storedApps.push(this.app);
            }else{
                storedApps.forEach((store: CacheApp) => {
                    store.selected = store.id === this.app.id;
                })
            }
        }
        if (this.option === 'edit') {
            storedApps.forEach((store: CacheApp) => {
                if (store.id === this.app.id) {
                    store.name = this.app.name;
                    store.url = this.app.url;
                    store.selected = this.app.selected;
                }
            })
        }
        if (this.option === 'delete') {
            if (exists) {
                storedApps.splice(storedApps.findIndex((store: CacheApp) => store.id === this.app.id), 1);
            }
        }
        localStorage.setItem('selectedApps', JSON.stringify(storedApps));
        return storedApps;
    }

}